//package com.yunxin.core.application.service.impl;
//
//import cn.hutool.core.text.CharSequenceUtil;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.yunxin.common.exception.BusinessException;
//import com.yunxin.common.result.PageResult;
//import com.yunxin.common.result.ResultCode;
//import com.yunxin.common.utils.JacksonUtils;
//import com.yunxin.core.application.assembler.ChannelAssembler;
//import com.yunxin.core.application.command.channel.*;
//import com.yunxin.core.application.dto.channel.*;
//import com.yunxin.core.application.service.ChannelQueryAdminService;
//import com.yunxin.core.infra.persistence.entity.channel.*;
//import com.yunxin.core.infra.persistence.entity.channel.query.condition.ChannelPageCondition;
//import com.yunxin.core.infra.persistence.entity.channel.query.condition.ChannelPromotionPageCondition;
//import com.yunxin.core.infra.persistence.entity.channel.query.condition.ChannelTypePageCondition;
//import com.yunxin.core.infra.persistence.entity.channel.query.result.*;
//import com.yunxin.core.infra.persistence.entity.product.ProductInfoDO;
//import com.yunxin.core.infra.persistence.mapper.channel.*;
//import com.yunxin.core.infra.persistence.mapper.product.ProductInfoMapper;
//import com.yunxin.core.utils.infra.utils.DateFormatUtils;
//import com.yunxin.core.utils.infra.utils.PageQueryUtils;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Service;
//
//import java.math.BigDecimal;
//import java.util.*;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//
///**
// * @author zero°
// * @since 2023-07-12
// */
//@Service
//@RequiredArgsConstructor
//public class ChannelQueryAdminServiceImpl implements ChannelQueryAdminService {
//
//    private final ChannelInfoExtendMapper channelInfoExtendMapper;
//
//    private final ChannelPromotionExtendMapper channelPromotionExtendMapper;
//
//    private final ChannelPromotionLinkConfigExtendMapper channelPromotionLinkConfigExtendMapper;
//
//    private final ChannelPromotionCustomConfigExtendMapper channelPromotionCustomConfigExtendMapper;
//
//    private final ChannelPromotionUnitPriceConfigExtendMapper channelPromotionUnitPriceConfigExtendMapper;
//
//    private final ChannelPromotionUnitPriceConfigVersionExtendMapper channelPromotionUnitPriceConfigVersionExtendMapper;
//
//    private final ChannelTypeExtendMapper channelTypeExtendMapper;
//
//    private final ChannelPromotionApiCustomerConfigExtendMapper channelPromotionApiCustomerConfigExtendMapper;
//
//    private final ChannelPromotionMerchantConfigExtendMapper channelPromotionMerchantConfigExtendMapper;
//
//    private final ChannelAdvanceChargeRecordExtendMapper channelAdvanceChargeRecordExtendMapper;
//
//    private final ChannelAssembler channelAssembler;
//
//    private final ChannelProhibitProductMapper channelProhibitProductMapper;
//
//    private final ProductInfoMapper productInfoMapper;
//
//    @Override
//    public ChannelDetailDTO getChannelDetail(Long id) {
//        ChannelInfoResult result = channelInfoExtendMapper.getChannelDetail(id);
//        if (Objects.isNull(result)) {
//            return null;
//        }
//        return channelAssembler.toChannelDetailDTO(result);
//    }
//
//    @Override
//    public List<ChannelListDTO> listChannel() {
//        List<ChannelListResult> results = channelInfoExtendMapper.listChannel();
//        return results.stream().map(channelAssembler::toChannelListDTO).toList();
//    }
//
//    @Override
//    public PageResult<ChannelPageDTO> channelPageQuery(ChannelPageCommand command) {
//        ChannelPageCondition condition = channelAssembler.fromChannelPageCommand(command);
//        long total = channelInfoExtendMapper.countChannelPageQuery(condition);
//        if (total == 0) {
//            return PageResult.success(Collections.emptyList(), total);
//        }
//        condition.setOffset(PageQueryUtils.X.getOffset(command.getPageNum(), command.getPageSize()));
//        List<ChannelInfoResult> results = channelInfoExtendMapper.channelPageQuery(condition);
//        List<ChannelPageDTO> dtoList = results.stream().map(channelAssembler::toChannelPageDTO).toList();
//        return PageResult.success(dtoList, total);
//    }
//
//    @Override
//    public ChannelPromotionBasicInfoDTO getChannelPromotionBasicInfo(Long promotionId) {
//        ChannelPromotionBasicInfoResult result = channelPromotionExtendMapper.selectByPromotionId(promotionId);
//        if (Objects.isNull(result)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        ChannelPromotionBasicInfoDTO basicInfoDTO = channelAssembler.toChannelPromotionBasicInfoDTO(result);
//        basicInfoDTO.setProtocolStatus(result.getProtocolStatus());
//
//        return basicInfoDTO;
//    }
//
//    @Override
//    public ChannelPromotionLinkConfigDTO getChannelPromotionLinkConfig(Long promotionId) {
//        ChannelPromotionLinkConfigDO linkConfig = channelPromotionLinkConfigExtendMapper.selectByPromotionId(promotionId);
//        if (Objects.isNull(linkConfig)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        return channelAssembler.toChannelPromotionLinkConfigDTO(linkConfig);
//    }
//
//    @Override
//    public ChannelPromotionCustomConfigDTO getChannelPromotionCustomConfig(Long promotionId) {
//        ChannelPromotionCustomConfigDO customConfig = channelPromotionCustomConfigExtendMapper.selectByPromotionId(promotionId);
//        if (Objects.isNull(customConfig)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        return channelAssembler.toChannelPromotionCustomConfigDTO(customConfig);
//    }
//
//    @Override
//    public ChannelPromotionUnitPriceConfigDTO getChannelPromotionUnitPriceConfig(Long promotionId) {
//        ChannelPromotionUnitPriceConfigDO unitPriceConfig = channelPromotionUnitPriceConfigExtendMapper.selectByPromotionId(promotionId);
//        if (Objects.isNull(unitPriceConfig)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        return channelAssembler.toChannelPromotionUnitPriceConfigDTO(unitPriceConfig);
//    }
//
//
//    @Override
//    public PageResult<ChannelPromotionPageDTO> channelPromotionPageQuery(ChannelPromotionPageCommand command) {
//        ChannelPromotionPageCondition condition = channelAssembler.fromChannelPromotionPageCommand(command);
//        long total = channelPromotionExtendMapper.countChannelPromotionPageQuery(condition);
//        if (total == 0) {
//            return PageResult.success(Collections.emptyList(), total);
//        }
//        condition.setOffset(PageQueryUtils.X.getOffset(command.getPageNum(), command.getPageSize()));
//        List<ChannelPromotionPageResult> results = channelPromotionExtendMapper.channelPromotionPageQuery(condition);
//        List<ChannelPromotionPageDTO> dtoList = results.stream().map(channelAssembler::toChannelPromotionPageDTO).toList();
//
//        dtoList.forEach(x->{
//            ArrayList<Long> ids = new ArrayList<>();
//            ArrayList<String> names = new ArrayList<>();
//            Long channelId = x.getChannelId();
//            List<ChannelProhibitProduct> list = channelProhibitProductMapper.listChannelProhibitProduct(channelId);
//
//            list.forEach(info->{
//                ProductInfoDO productInfoDO = productInfoMapper.selectByPrimaryKey(info.getProductId());
//                ids.add(info.getProductId());
//                names.add(productInfoDO.getProductName());
//            });
//            x.setProductId(ids);
//            x.setProductName(names);
//
//        });
//        return PageResult.success(dtoList, total);
//    }
//
//
//
//    @Override
//    public List<ChannelTypeListDTO> listChannelType() {
//        List<ChannelTypeListResult> results = channelTypeExtendMapper.listChannelType();
//        return results.stream().map(channelAssembler::toChannelTypeListDTO).toList();
//    }
//
//    @Override
//    public PageResult<ChannelTypePageDTO> channelTypePageQuery(ChannelTypePageCommand command) {
//        ChannelTypePageCondition condition = channelAssembler.fromChannelTypePageCommand(command);
//        long total = channelTypeExtendMapper.countChannelTypePageQuery(condition);
//        if (total == 0) {
//            return PageResult.success(Collections.emptyList(), total);
//        }
//        condition.setOffset(PageQueryUtils.X.getOffset(command.getPageNum(), command.getPageSize()));
//        List<ChannelTypeDO> list = channelTypeExtendMapper.channelTypePageQuery(condition);
//        List<ChannelTypePageDTO> dtoList = list.stream().map(channelAssembler::toChannelTypePageDTO).toList();
//        return PageResult.success(dtoList, total);
//    }
//
//    @Override
//    public List<ChannelPromotionChargedListDTO> listChannelPromotionCharged(Long promotionId) {
//        List<ChannelPromotionUnitPriceConfigVersionDO> list = channelPromotionUnitPriceConfigVersionExtendMapper.listChannelPromotionUnitPriceConfigVersionByPromotionId(promotionId);
//        return list.stream().map(channelAssembler::toChannelPromotionChargedListDTO).toList();
//    }
//
//    @Override
//    public List<ChannelPromotionChargedInfoDTO> listChannelPromotionCharged(Set<String> channelPromotionCodes) {
//        List<ChannelPromotionChargedInfoResult> list = channelPromotionUnitPriceConfigExtendMapper.listByPromotionCodes(channelPromotionCodes);
//        return list.stream().map(channelAssembler::toChannelPromotionChargedInfoDTO).toList();
//    }
//
//    @Override
//    public ChannelPromotionApiCustomerConfigDTO getChannelPromotionApiCustomerConfig(String promotionCode) {
//        ChannelPromotionApiCustomerConfigDO channelPromotionApiCustomerConfigDO = channelPromotionApiCustomerConfigExtendMapper.selectChannelPromotionApiCustomerConfigByPromotionCode(promotionCode);
//        return channelAssembler.toChannelPromotionApiCustomerConfigDTO(channelPromotionApiCustomerConfigDO);
//    }
//
//    @Override
//    public ChannelPromotionMerchantConfigDTO getChannelPromotionMerchantConfig(String promotionCode) {
//        ChannelPromotionMerchantConfigDO channelPromotionMerchantConfigDO = channelPromotionMerchantConfigExtendMapper.selectChannelPromotionMerchantConfigByChannelCode(promotionCode);
//        if(Objects.isNull(channelPromotionMerchantConfigDO)) {
//            return null;
//        }
//        ChannelPromotionMerchantConfigDTO merchantConfigDTO = new ChannelPromotionMerchantConfigDTO();
//        merchantConfigDTO.setId(channelPromotionMerchantConfigDO.getId());
//        merchantConfigDTO.setChannelPromotionCode(channelPromotionMerchantConfigDO.getChannelPromotionCode());
//        merchantConfigDTO.setAppointMerchants(CharSequenceUtil.isNotBlank(channelPromotionMerchantConfigDO.getAppointMerchants()) ? JacksonUtils.X.parse(channelPromotionMerchantConfigDO.getAppointMerchants(), new TypeReference<>() {}) : null);
//        merchantConfigDTO.setFilterMerchants(JacksonUtils.X.parse(channelPromotionMerchantConfigDO.getFilterMerchants(), new TypeReference<>() {}));
//        return merchantConfigDTO;
//    }
//
//    @Override
//    public ChannelAdvanceChargeRecordDetailDTO getChannelAdvanceChargeRecordDetail(Long id) {
//        ChannelAdvanceChargeRecordDO channelAdvanceChargeRecordDO = channelAdvanceChargeRecordExtendMapper.selectByPrimaryKey(id);
//        return channelAssembler.toChannelAdvanceChargeRecordDetailDTO(channelAdvanceChargeRecordDO);
//    }
//
//    @Override
//    public PageResult<ChannelAdvanceChargeRecordPageDTO> channelAdvanceChargeRecordPageQuery(ChannelAdvanceChargeRecordPageCommand command) {
//        Long channelId = command.getChannelId();
//        Date startDate = DateFormatUtils.X.getDateByStr(command.getStartAdvanceChargeDate());
//        Date endDate = DateFormatUtils.X.getDateByStr(command.getEndAdvanceChargeDate());
//        long total = channelAdvanceChargeRecordExtendMapper.countChannelAdvanceChargeRecordPageQuery(channelId, startDate, endDate);
//        if (total == 0) {
//            return PageResult.success(Collections.emptyList(), total);
//        }
//        int offset = PageQueryUtils.X.getOffset(command.getPageNum(), command.getPageSize());
//        List<ChannelAdvanceChargeRecordDO> results = channelAdvanceChargeRecordExtendMapper.channelAdvanceChargeRecordPageQuery(channelId, startDate, endDate, offset, command.getPageSize());
//        List<ChannelAdvanceChargeRecordPageDTO> dtoList = results.stream().map(channelAssembler::toChannelAdvanceChargeRecordPageDTO).toList();
//        return PageResult.success(dtoList, total);
//    }
//
//    @Override
//    public ChannelAdvanceChargeRecordStatisticsDTO channelAdvanceChargeRecordStatistics(ChannelAdvanceChargeRecordPageCommand command) {
//        Long channelId = command.getChannelId();
//        Date startDate = DateFormatUtils.X.getDateByStr(command.getStartAdvanceChargeDate());
//        Date endDate = DateFormatUtils.X.getDateByStr(command.getEndAdvanceChargeDate());
//        ChannelAdvanceChargeRecordStatisticsResult result = channelAdvanceChargeRecordExtendMapper.channelAdvanceChargeRecordStatistics(channelId, startDate, endDate);
//        if (Objects.isNull(result)) {
//            return null;
//        }
//        ChannelAdvanceChargeRecordStatisticsDTO dto = new ChannelAdvanceChargeRecordStatisticsDTO();
//        dto.setTotalConsumedAmount(result.getTotalConsumedAmount());
//        dto.setTotalResidueAdvanceChargeAmount(result.getTotalAdvanceChargeAmount().subtract(result.getTotalConsumedAmount()));
//        return dto;
//    }
//
//    @Override
//    public PageResult<ChannelAdvanceChargePageDTO> channelAdvanceChargePageQuery(ChannelAdvanceChargePageCommand command) {
//        ChannelPageCondition condition = new ChannelPageCondition();
//        condition.setChannelId(command.getChannelId());
//        long total = channelInfoExtendMapper.countChannelPageQuery(condition);
//        if (total == 0) {
//            return PageResult.success(Collections.emptyList(), total);
//        }
//        condition.setOffset(PageQueryUtils.X.getOffset(command.getPageNum(), command.getPageSize()));
//        condition.setPageSize(command.getPageSize());
//        condition.setStartTime(command.getStartTime());
//        condition.setEndTime(command.getEndTime());
//        List<ChannelInfoResult> results = channelInfoExtendMapper.channelPageQuery(condition);
//        if(results.isEmpty()){
//            return PageResult.success(Collections.emptyList(), 0);
//        }
//
//        Set<Long> channelIds = results.stream().map(ChannelInfoResult::getId).collect(Collectors.toSet());
//        Map<Long, ChannelAdvanceChargeResult> advanceChargeMap = channelAdvanceChargeRecordExtendMapper.listChannelAdvanceChargeResultByChannelIds(channelIds).stream().collect(Collectors.toMap(ChannelAdvanceChargeResult::getChannelId, Function.identity()));
//        List<ChannelAdvanceChargePageDTO> dtoList =  results.stream().map(x -> {
//        ChannelAdvanceChargePageDTO channelAdvanceChargePageDTO = new ChannelAdvanceChargePageDTO();
//        channelAdvanceChargePageDTO.setChannelName(x.getChannelName());
//        channelAdvanceChargePageDTO.setChannelId(x.getId());
//        channelAdvanceChargePageDTO.setChannelTypeName(x.getChannelTypeName());
//        channelAdvanceChargePageDTO.setCreateTime(x.getCreateTime());
//        channelAdvanceChargePageDTO.setSettlementNumber(0);
//        channelAdvanceChargePageDTO.setSettlementAmount(BigDecimal.ZERO);
//        channelAdvanceChargePageDTO.setAdvanceChargeAmount(BigDecimal.ZERO);
//        channelAdvanceChargePageDTO.setConsumedAmount(BigDecimal.ZERO);
//        channelAdvanceChargePageDTO.setResidueAdvanceChargeAmount(BigDecimal.ZERO);
//        ChannelAdvanceChargeResult channelAdvanceChargeResult = advanceChargeMap.get(x.getId());
//        Optional.ofNullable(channelAdvanceChargeResult).ifPresent(ca -> {
//            channelAdvanceChargePageDTO.setSettlementNumber(ca.getSettlementNumber());
//            channelAdvanceChargePageDTO.setSettlementAmount(ca.getSettlementAmount());
//            channelAdvanceChargePageDTO.setAdvanceChargeAmount(ca.getAdvanceChargeAmount());
//            channelAdvanceChargePageDTO.setConsumedAmount(channelAdvanceChargePageDTO.getSettlementAmount());
//            channelAdvanceChargePageDTO.setResidueAdvanceChargeAmount(channelAdvanceChargePageDTO.getAdvanceChargeAmount().subtract(channelAdvanceChargePageDTO.getConsumedAmount()));
//        });
//        return channelAdvanceChargePageDTO;
//    }).toList();
//
//        return PageResult.success(dtoList, total);
//    }
//
//
//}
